package com.fudan.adse.component;


import com.fudan.adse.domain.dto.AdminUserDetails;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * JWT 内容增强器，填充额外信息
 *
 * @author Jerry Zhang
 * create: 2022-10-28 15:10
 */
@Component
public class JwtTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
        AdminUserDetails adminUserDetails = (AdminUserDetails) oAuth2Authentication.getPrincipal();
        Map<String, Object> idInfo = new HashMap<>();
        idInfo.put("username", adminUserDetails.getUsername());
        idInfo.put("client_id", adminUserDetails.getClient());
        ((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(idInfo);
        return oAuth2AccessToken;
    }
}
